// Problem 177: Integer angled Quadrilaterals

// ref: rokusuke's solution: Using brute force to try all combinations (CAD, BAC, ABD, CBD) s.t. CAD is the minimum.

package main

import (
	"fmt"
	"math"
)

var sin [181]float64
var cos [181]float64

func p177() {
	var a, b, c, d, e, f, g, h, hc int
	var x, y, z, df float64
	count := 0
	init177()
	for a = 1; a <= 45; a++ {
		for b = a; b <= 180-a*3; b++ {
			for c = a; c <= 180-2*a-b; c++ {
				for d = a; d <= 180-a-b-c; d++ {
					e = 180 - (b + c + d)
					h = 180 - (a + b + c)
					x = sin[c+d] / sin[e]
					y = sin[c] / sin[h]
					z = math.Sqrt(x*x + y*y - 2*x*y*cos[a])
					df = math.Acos((x*x+z*z-y*y)/(2*x*z)) * 180 / math.Pi
					f = (int)(df + 0.5)
					if math.Abs(df-float64(f)) > 1e-9 {
						continue
					}
					if f < a {
						break
					}
					g = 180 - (a + f + h)
					if g < a {
						break
					}
					hc = keyString177(a, b, c, d)
					if hc > keyString177(c, d, e, f) {
						continue
					}
					if hc > keyString177(e, f, g, h) {
						continue
					}
					if hc > keyString177(g, h, a, b) {
						continue
					}
					if hc > keyString177(b, a, h, g) {
						continue
					}
					if hc > keyString177(d, c, b, a) {
						continue
					}
					if hc > keyString177(f, e, d, c) {
						continue
					}
					if hc > keyString177(h, g, f, e) {
						continue
					}
					count++
				}
			}
		}
	}

	fmt.Println("Problem 177:", count)
}
func init177() {
	for x := 0.0; x <= 180.0; x++ {
		sin[int(x)] = math.Sin(x * math.Pi / 180)
		cos[int(x)] = math.Cos(x * math.Pi / 180)
	}
}

func keyString177(a, b, c, d int) int {
	return (a << 24) | (b << 16) | (c << 8) | d
}
